package com.googlecode.animator.view.materialslibrary
{
	import com.googlecode.data.FileObjectVO;
	import com.googlecode.animator.data.Notifications;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.controls.Button;
	
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	/**
	 * Ties in load button with PureMVC framework. 
	 * @author USER
	 * 
	 */
	public class MaterialsButtonMediator extends Mediator
	{
		/**
		 * File. 
		 */
		protected var _file:File;
		
		/**
		 * Stores a reference to the type of file being loaded, according to which button was clicked.
		 * This is not 100% gauranteed to be correct. 
		 */
		protected var _isFLV:Boolean;
		
		/**
		 * Constructor. 
		 * @param viewComponent
		 * 
		 */
		public function MaterialsButtonMediator(name:String, viewComponent:Button)
		{
			super(name, viewComponent);
			
		}
		
		/**
		 * Called by PureMVC framework. 
		 * 
		 */
		override public function onRegister():void
		{
			super.onRegister();
			
			viewRef.addEventListener(MouseEvent.CLICK, onLoadButtonClick);
		}
		
		/**
		 * Responds to a user clicking on the button used to load materials. 
		 * @param evt
		 * 
		 */
		protected function onLoadButtonClick(evt:MouseEvent):void
		{	
			// override this function to set file functions
			if(evt.target is MaterialsFLVButton)
			{
				_isFLV = true;
			}else
			{
				_isFLV = false;
			}
		}
		
		/**
		 * Sends a FileObjectVO with the file path selected by the user. 
		 * @param evt
		 * 
		 */
		protected function onFileSelect(evt:Event):void
		{
			_file.removeEventListener(Event.SELECT, onFileSelect);
			
			var folder:File = _file.parent;
			
			var VO:FileObjectVO = new FileObjectVO();
			VO.filePath = _file.nativePath;
			VO.isFLV = _isFLV;
			
			sendNotification(Notifications.MATERIALS_FILE_PATH_SELECTED, VO);
			
			if(folder.isDirectory)
			{
				_file = folder;
			}
		}
		
		/**
		 * Returns typed reference to component. 
		 * @return 
		 * 
		 */
		protected function get viewRef():Button
		{
			return viewComponent as Button;
		}
	}
}